package com.vnet.bdz.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.RootPanel;
import com.gwtext.client.core.XTemplate;
import com.gwtext.client.data.FieldDef;
import com.gwtext.client.data.RecordDef;
import com.gwtext.client.data.Store;
import com.gwtext.client.data.StringFieldDef;
import com.gwtext.client.data.XmlReader;
import com.gwtext.client.widgets.DataView;
import com.gwtext.client.widgets.MessageBox;

public class Artykuly implements EntryPoint {

		final DataView view = new DataView("div.galeria");
		@Override
		public void onModuleLoad() {
			try {
				setupView();
				RootPanel.get("view").add(view);
			} catch (Exception e) {
				MessageBox.alert(e.getMessage());
				
			}
		}
		
		private void setupView() {
	        XmlReader reader = new XmlReader("item", new RecordDef(   
	                new FieldDef[]{   
	                        new StringFieldDef("description","/description"),
	                        new StringFieldDef("title","/title")
	                }   
	        ));   
	        
	        SiteServiceAsync siteService = GWT.create(SiteService.class);
	        final Store store = new Store(reader);
	        view.setStore(store);

	        final XTemplate wtemplate = new XTemplate(new String[]{   
	        		"<table class='galariatab'><tpl for='.'>",
	        		"<tr><td><div class='galeria'>",
	        		"<span class='header'>{title}</span><br>",
	        		"<span class='description'>{description}</span>",
	                "</div></td></tr>",
	                "</tpl></table>"});  

	        view.setTpl(wtemplate);

	        AsyncCallback<String> callback = new AsyncCallback<String>() 
	        {
				@Override
				public void onFailure(Throwable caught) {
					// TODO Auto-generated method stub
				}

				@Override
				public void onSuccess(String result) {
					try
					{
						store.loadXmlData(result, false);
					} catch (Exception e) {
						MessageBox.alert(e.getMessage());
					}
				}  
	        };

	        siteService.getBlogXML("http://klubbiegaczakatywroclawskie.blogspot.com/feeds/posts/default?alt=rss",callback);
	}

}
